$(function(){
    isWeixin && configWechat();
    //defaultBizSource();
    initForm();
    initBtn();
    initShare();
    // checkUserOrders();
    initMarquee();
    recommendShow();
    lxmRPSubmit('3c5da757-d321-11e6-a47f-00163e02bee5');
});

function changeThemeTitle(){
    var cardTitles = ['TA真心爱我吗？', '和TA会不会有结果？', '该不该重新开始？', '桃花什么时候来？'];
    var cardType = $('#cards .active').attr('datacard');
    var sexTxt = $('#userSex .radio-girl').hasClass('active') ? '他' : '她';
    $('#subTitle').text('占卜主题：' + cardTitles[cardType].replace(/TA/g, sexTxt));
}

function initBtn(){
    $('#orders').click(function(){
        window.location.href = 'orderlist.html';
    });

    $('#check, #btnFloat').click(function(){
        if ($(this).attr('id') == 'check') {
            lxmRPSubmit('fb164833-1ab4-472a-8e20-84336f99053e');
        } else {
            $(window).scrollTop($('#circles').offset().top);
            lxmRPSubmit('468a6ff4-b473-47d0-938c-6b6e23e1a8c4');
        }
        // 表单验证
        var formData = checkForm();
        if (!formData.valid) {
            return;
        }
        window.location.href = 'drawcard.html';
    });

    // 选择性别
    $('#userSex .radio-btn, #userStatus .radio-btn').click(function(){
        $(this).parent().children('.radio-btn').removeClass('active');
        $(this).addClass('active');
        changeThemeTitle();
    });

    // 卡牌类型
    $('#cards .card').click(function(){
        $(this).parent().children('.card').removeClass('active');
        $(this).addClass('active');
        changeThemeTitle();
        // var cardType = $(this).attr('datacard');
        
        // var sexTxt = $('#userSex .radio-girl').hasClass('active') ? '他' : '她';
        // $('#subTitle').text('占卜主题：' + cardTitles[cardType].replace(/TA/g, sexTxt));
    });

    // 页面滚动
    window.onscroll = function(){
        clearTimeout(LXMBase.data.btnFoot);
        LXMBase.data.btnFoot = setTimeout(function(){
            // 滚动高度
            var sHeight = document.documentElement.scrollTop || document.body.scrollTop,
                showBtnPoint = $('#check').offset().top + $('#check').height();
            if (sHeight > showBtnPoint) {
                $('#btnFloat').show();
            } else {
                $('#btnFloat').hide();
            }
        }, 200);
    };
}

// 核查表单信息
function checkForm() {
    var formData = {valid: true};

    // 自己
    var sexTxt = '女',
        statusTxt = '非单身';
    // 错误信息/错误码
    var errorTxtArr = [];
    var validCode = 0;
    // 验证性别
    if ($('#userSex .radio-girl').hasClass('active')) {
        sexTxt = '女';
    } else {
        sexTxt = '男';
    }
    // 验证情感状态
    if ($('#userStatus .radio-double').hasClass('active')) {
        statusTxt = '非单身';
    } else {
        statusTxt = '单身';
    }
    // 验证类型
    var cardType = $('#cards .active').attr('datacard');

    formData.sexTxt = sexTxt;
    formData.sex = (sexTxt == '女') ? 0 : 1;
    formData.statusTxt = statusTxt;
    formData.status = (statusTxt == '非单身') ? 0 : 1;
    formData.card = cardType;
    LXMBase.setObjItem('tr_data', formData);

    if (!formData.valid) {
        (errorTxtArr.length > 0) && alertMsg(errorTxtArr[0]);
        return formData;
    }
    return formData;
}

function initForm() {
    var data = LXMBase.getObjItem('tr_data');
    if (!data) {return;}
    // 性别
    $('#userSex .radio-btn').removeClass('active');
    if (data.sex == 1) {
        $('#userSex .radio-boy').addClass('active');
    } else {
        $('#userSex .radio-girl').addClass('active');
    }
    // 感情状态
    $('#userStatus .radio-btn').removeClass('active');
    if (data.status == 1) {
        $('#userStatus .radio-single').addClass('active');
    } else {
        $('#userStatus .radio-double').addClass('active');
    }
    // 类型
    if (!isNaN(data.card)) {
        $('#cards .card').removeClass('active');
        $('#cards .card' + data.card).addClass('active');
    }
    changeThemeTitle();
}

function checkUserOrders() {
    var uid = LXMBase.getItem('biz_user_id'),
        bizSource = LXMBase.getItem('biz_source');
    if (!uid || !bizSource) {
        return;
    }
    var param = {
        uid: '', tk: '',
        body: {
            biz_id: biz_id_map[bizSource],
            biz_source: bizSource,
            query_count: 3,
            query_days: 2
        }
    };
    if (LXMBase.isPhone(uid)) {
        param.body.phonestatus = 1;
        param.body.biz_user_phone = uid;
    } else {
        param.body.biz_user_id = uid;
    }
    $.ajax({
        type: 'post',
        url: httpsvr + '/proreport/queryAllProreports.do',
        data: JSON.stringify(param),
        success: function(res) {
            // console.log(res)
            if (res.code == 'S_OK' && res.var && res.var.list) {
                var tmpList = res.var.list;
                for (var i = 0; i < tmpList.length; i++) {
                    if (tmpList[i].flag == 0 || tmpList[i].flag == 2) {
                        $('#orders').addClass('btn-locked');
                        $('#orders').addClass('btn-active');
                        break;
                    }
                }
            }
        },
        error: function() {}
    });
}

function initMarquee() {
    var datas = [
        {phone: '137******43', address: '深圳', content: '解答很详细，说的也很准'},
        {phone: '137******92', address: '贵州', content: '准到不行哦！！！几乎每次有感情问题都来寻求帮助，现在和男朋友的感情稳定了很多，谢谢，我情感生活的完美工具'},
        {phone: '137******03', address: '哈尔滨', content: '特别特别有用，那我心里的问题都解决了，是真实的一次面对自己，有感情问题再回来占'},
        {phone: '137******56', address: '台湾', content: '一直被困扰的问题解决了，前段时间只是她前女友想符合，但是他果断拒绝了，还和她说他现在有我很幸福'},
        {phone: '137******64', address: '香港', content: '已经习惯了有他的日子，测算说我们会有好结果，感觉心里落地了一半'},
        {phone: '137******32', address: '香港', content: '我是一个心粗的菇凉，很多他的生活细节都没关注，谢谢告诉我他喜欢什么，决定复合了'},
        {phone: '137******75', address: '广州', content: '很准啊，就是很准，能说到问题的关键'},
        {phone: '137******22', address: '贵州', content: '激动ing！激动ing！我们未来的发展一片光明，已看到我们一起慢慢变老的画面了'},
        {phone: '137******00', address: '桂林', content: '里面有很多建议，每天都可以感受到我们感情的提升'},
        {phone: '137******45', address: '柳州', content: '很专业！'},
        {phone: '137******88', address: '柳州', content: '好评！'},
        {phone: '137******32', address: '柳州', content: '答案很详细，准确捕捉到细节和关键点，很能疏解人心，往有意义的方向去帮助，准备在问个发展'},
        {phone: '137******45', address: '柳州', content: '非常有用，不仅有用还给我非常客观的建议，终于不用在要不要复合上面徘徊了'},
        {phone: '185******66', address: '深圳', content: '最近一直苦恼他到底怎么了，渐渐在怀疑他的真心，看了客观的结果，终于有了答案 '},
        {phone: '185******86', address: '深圳', content: '不管怎么样，解开了我心里的疑惑，非常期待有职业方面的占卜'},
        {phone: '185******35', address: '深圳', content: '他前女友是他以前高中的同学，一直都在怀疑他，谢谢报告的指引，爱需要信任'},
        {phone: '188******23', address: '深圳', content: '以前他玩起游戏就容易忽视我，慢慢学着懂他，感觉他回到了小哥哥的时候'},
        {phone: '185******75', address: '深圳', content: '内容很丰富，很多对未来的预测跟建议。'},
        {phone: '188******90', address: '东莞', content: '解牌马上就看出了问题的关键，说的很到位，感觉心里通透了好多，后期有问题还会再来哒，点个赞'},
        {phone: '188******56', address: '东莞', content: '很准！'},
        {phone: '188******33', address: '东莞', content: '一直以来都觉得和他性格匹配上有点问题，报告终于给了我答案，相处需要技巧'},
        {phone: '188******22', address: '东莞', content: '真的很准耶～真的太准了！'},
        {phone: '188******86', address: '东莞', content: '真是多亏了这次测算，让我更加坚定信念！还有，他的确喜欢大大的抱熊'},
        {phone: '188******97', address: '梅州', content: '说的很准，一个虽然不好的结果，也许这就是天意，顺其自然吧 '},
        {phone: '134******76', address: '珠海', content: '准确！是很好的感情指导工具'},
        {phone: '134******86', address: '杭州', content: '在这之前，我都从来没有把他介绍给我的闺蜜朋友，因为不确定是不是好真爱，看了报告，他就是我坚定的未来'},
        {phone: '134******88', address: '杭州', content: '闺蜜之前和她男朋友都是三天一小吵，五天一大吵，现在两人可会虐狗了，这个就是她推荐我来的'},
        {phone: '134******06', address: '杭州', content: '真是多亏了这次测算，让我更加坚定信念！'},
        {phone: '134******04', address: '杭州', content: '给了很多很多建议，现在越来越懂他了'},
        {phone: '136******32', address: '上海', content: '努力尝试了，也去追寻了该去寻找新的人生了'},
        {phone: '136******34', address: '上海', content: '挺好的，其实本身我自己就已经有决定了，像找个人来增加作决定的坚定性'},
        {phone: '136******89', address: '上海', content: '好'},
        {phone: '136******33', address: '上海', content: '非常好，现在有感情问题就来测，我还把工具添加到了微信收藏，这样就好方便找了'},
        {phone: '136******54', address: '佛山', content: '很准，事事都说到心里去了'},
        {phone: '136******33', address: '贵阳', content: '建议真的很中肯，抽牌感觉是一种舒心，不会有被看穿心事的压迫感，很舒服，自助工具就是好'},
        {phone: '136******67', address: '长沙', content: '因为一点小事我们冷战了好多天，后来朋友推了这个测试给我，只能说很准！'},
        {phone: '186******88', address: '武汉', content: '我们经常因为莫名其妙的事情吵架，我一直觉得我们是不适合的，差点想放弃，看了结果之后决定尝试多了解和体谅'},
        {phone: '186******23', address: '武汉', content: '和他在一起很久了，感觉很平凡觉得有点没意思了，结果给了一点小指示，现在感觉找回了初恋的感觉哈哈哈'},
        {phone: '186******44', address: '武汉', content: '刚刚占扑，真心不错，以前也做过塔罗牌，后面找不到老师了，有了这个工具方便多了，有疑问问问，哈哈哈'},
        {phone: '186******43', address: '武汉', content: '复合复合复合，重要事情说三遍，这指引对我太关键了'},
        {phone: '186******29', address: '长治', content: '之前吵架了不想理他，测试报告让我觉得要珍惜，现在很幸福'},
        {phone: '186******09', address: '赣州', content: '本来就很幸福看完结果后觉得我们会更幸福哈哈哈哈哈哈'},
        {phone: '186******33', address: '厦门', content: '遇到这个测试真的是太及时了！！急需啊！！！'},
        {phone: '157******23', address: '秦皇岛', content: '朋友和她男朋友遇到了点问题，果断推给了她'},
        {phone: '157******43', address: '大连', content: '很详细！'},
        {phone: '157******21', address: '唐山', content: '前段时间他一直没有时间陪我，就有点多想了，测试完之后觉得安心好多，现在贼幸福！'},
        {phone: '157******95', address: '苏州', content: '好准！想给个小心心，我要去找我男朋友了哈哈哈'},
        {phone: '157******23', address: '武汉', content: '测试结果让我对我们的未来更有信心了'},
        {phone: '157******56', address: '常州', content: '一直以来对他都很放心，看来以后会对他更放心哈哈'}
    ];

    var index, temp;
    // 打乱数组
    for (var i = 0; i <= datas.length; i++) {
        // 产生从 i 到 length 之间的随机数
        index = parseInt(Math.random() * (datas.length - i)) + i;
        if (index != i) {
            temp = datas[i];
            datas[i] = datas[index];
            datas[index] = temp;
        }
    }

    var t = [];
    for(var j = 0; j < datas.length; j++) {
        var tmpData = datas[j];
        t.push('<li class="users-item">');
        t.push(  '<div class="info">' + tmpData.phone + ' ' + tmpData.address + '</div>');
        t.push(  '<div class="txt">' + tmpData.content + '</div>');
        t.push('</li>');
    }
    $('#userlist').html(t.join(''));
    doMarquee();
}

function doMarquee() {
    clearTimeout(LXMBase.data.marquee);
    LXMBase.data.marquee = setTimeout(function(){
        var tmpHeight = $('#userlist').children('li.users-item').eq(0).height();
        $('#userlist').animate({top: '-' + tmpHeight + 'px'}, 2000, function(){
            var tmpfistItemDom = $('#userlist').children('li.users-item').eq(0);
            $('#userlist').css('top', '0px');
            tmpfistItemDom.appendTo($('#userlist'));
            doMarquee();
        });
    }, 500);
}

function recommendShow(){
    // var bazi_source = 'lxmsglclassic2',
    //     zhiye_source = 'lxmonejob2',
    //     love_source = 'lxmqlove2',
    //     caifu_source = 'lxmwealthsdk2';
    // var biz_source = LXMBase.getItem('biz_source');
    // if(biz_source == 'testtarotasdk3'){
    //     bazi_source = 'testbsglbclassic2';
    //     zhiye_source = 'testbonejob2';
    //     love_source = 'testbqlove2';
    //     caifu_source = 'testwealthbsdk2';
    // }
    // if(isPush()){
    //     $('#hotReckon').show();
    // } else {
    //     $('#hotReckon').hide();
    // }

    $('.bazi').click(function(){
        var tmpSrc = $(this).attr('lxmsrc');
        window.location.href = websvr + '/sdkmodsingle/sdksingleclassic2/loading.html?biz_source=' + tmpSrc;
    })

    $('.love').click(function(){
        var tmpSrc = $(this).attr('lxmsrc');
        window.location.href = websvr + '/sdkpack/sdkloveplan/loading.html?biz_source=' + tmpSrc;
    })

    $('.zhiye').click(function(){
        var tmpSrc = $(this).attr('lxmsrc');
        window.location.href = websvr + '/sdkpack/sdkjob/loading.html?biz_source=' + tmpSrc;
    })

    $('.caifu').click(function(){
        var tmpSrc = $(this).attr('lxmsrc');
        window.location.href = websvr + '/sdkpack/sdkwealth2/loading.html?biz_source=' + tmpSrc;
    })

    var bizSource = LXMBase.getItem('biz_source');
    if (!bizSource) {
        return;
    }

    // 四个链接
    var srcDetailArr = [
        {srce: 'lxmtarotsdk3', arr: ['lxmsglclassic2', 'lxmqlove2', 'lxmonejob2', 'lxmwealthsdk2']}
    ];
    var needDetail = ['lxmsglclassic2', 'lxmqlove2', 'lxmonejob2', 'lxmwealthsdk2'];
    for (var j = 0; j < srcDetailArr.length; j++) {
        var tmp = srcDetailArr[j];
        if (bizSource == tmp.srce) {
            needDetail = tmp.arr;
            $('#hotReckon .bazi').attr('lxmsrc', needDetail[0]);
            $('#hotReckon .love').attr('lxmsrc', needDetail[1]);
            $('#hotReckon .zhiye').attr('lxmsrc', needDetail[2]);
            $('#hotReckon .caifu').attr('lxmsrc', needDetail[3]);
            break;
        }
    }

    // 需要推荐的渠道在这里配置
    var sourceArr = [
        'lxmtarotsdk3'
    ];
    var findit = false;
    for (var i = 0; i < sourceArr.length; i++) {
        var tmpSource = sourceArr[i];
        if (bizSource == tmpSource) {
            findit = true;
            break;
        }
    }

    if (findit) {
        $('#hotReckon').show();
    }
}

// function isPush() {
//     // 需要推荐的渠道在这里配置
//     var sourceArr = ['testtarotasdk3'];
//     var bizSource = LXMBase.getItem('biz_source');
//     if (!bizSource) {
//         return false;
//     }
//     var findit = false;
//     for (var i = 0; i < sourceArr.length; i++) {
//         var tmpSource = sourceArr[i];
//         if (bizSource == tmpSource) {
//             findit = true;
//             break;
//         }
//     }
//     return findit;
// }